registration: Convert "config" into an object with metadata
[lhc/web/wiklou.git] / includes / registration / ExtensionProcessor.php
1 <?php
2
3 class ExtensionProcessor implements Processor {
4
5 /**
6 * Keys that should be set to $GLOBALS
7 *
8 * @var array
9 */
10 protected static $globalSettings = [
11 'ResourceLoaderSources',
12 'ResourceLoaderLESSVars',
13 'DefaultUserOptions',
14 'HiddenPrefs',
15 'GroupPermissions',
16 'RevokePermissions',
17 'GrantPermissions',
18 'GrantPermissionGroups',
19 'ImplicitGroups',
20 'GroupsAddToSelf',
21 'GroupsRemoveFromSelf',
22 'AddGroups',
23 'RemoveGroups',
24 'AvailableRights',
25 'ContentHandlers',
26 'ConfigRegistry',
27 'SessionProviders',
28 'AuthManagerAutoConfig',
29 'CentralIdLookupProviders',
30 'RateLimits',
31 'RecentChangesFlags',
32 'MediaHandlers',
33 'ExtensionFunctions',
34 'ExtensionEntryPointListFiles',
35 'SpecialPages',
36 'JobClasses',
37 'LogTypes',
38 'LogRestrictions',
39 'FilterLogTypes',
40 'ActionFilteredLogs',
41 'LogNames',
42 'LogHeaders',
43 'LogActions',
44 'LogActionsHandlers',
45 'Actions',
46 'APIModules',
47 'APIFormatModules',
48 'APIMetaModules',
49 'APIPropModules',
50 'APIListModules',
51 'ValidSkinNames',
52 'FeedClasses',
53 ];
54
55 /**
56 * Mapping of global settings to their specific merge strategies.
57 *
58 * @see ExtensionRegistry::exportExtractedData
59 * @see getExtractedInfo
60 * @var array
61 */
62 protected static $mergeStrategies = [
63 'wgGroupPermissions' => 'array_plus_2d',
64 'wgRevokePermissions' => 'array_plus_2d',
65 'wgGrantPermissions' => 'array_plus_2d',
66 'wgHooks' => 'array_merge_recursive',
67 'wgExtensionCredits' => 'array_merge_recursive',
68 'wgExtraGenderNamespaces' => 'array_plus',
69 'wgNamespacesWithSubpages' => 'array_plus',
70 'wgNamespaceContentModels' => 'array_plus',
71 'wgNamespaceProtection' => 'array_plus',
72 'wgCapitalLinkOverrides' => 'array_plus',
73 'wgRateLimits' => 'array_plus_2d',
74 'wgAuthManagerAutoConfig' => 'array_plus_2d',
75 ];
76
77 /**
78 * Keys that are part of the extension credits
79 *
80 * @var array
81 */
82 protected static $creditsAttributes = [
83 'name',
84 'namemsg',
85 'author',
86 'version',
87 'url',
88 'description',
89 'descriptionmsg',
90 'license-name',
91 ];
92
93 /**
94 * Things that are not 'attributes', but are not in
95 * $globalSettings or $creditsAttributes.
96 *
97 * @var array
98 */
99 protected static $notAttributes = [
100 'callback',
101 'Hooks',
102 'namespaces',
103 'ResourceFileModulePaths',
104 'ResourceModules',
105 'ResourceModuleSkinStyles',
106 'ExtensionMessagesFiles',
107 'MessagesDirs',
108 'type',
109 'config',
110 'config_prefix',
111 'ParserTestFiles',
112 'AutoloadClasses',
113 'manifest_version',
114 'load_composer_autoloader',
115 ];
116
117 /**
118 * Stuff that is going to be set to $GLOBALS
119 *
120 * Some keys are pre-set to arrays so we can += to them
121 *
122 * @var array
123 */
124 protected $globals = [
125 'wgExtensionMessagesFiles' => [],
126 'wgMessagesDirs' => [],
127 ];
128
129 /**
130 * Things that should be define()'d
131 *
132 * @var array
133 */
134 protected $defines = [];
135
136 /**
137 * Things to be called once registration of these extensions are done
138 *
139 * @var callable[]
140 */
141 protected $callbacks = [];
142
143 /**
144 * @var array
145 */
146 protected $credits = [];
147
148 /**
149 * Any thing else in the $info that hasn't
150 * already been processed
151 *
152 * @var array
153 */
154 protected $attributes = [];
155
156 /**
157 * @param string $path
158 * @param array $info
159 * @param int $version manifest_version for info
160 * @return array
161 */
162 public function extractInfo( $path, array $info, $version ) {
163 if ( $version === 2 ) {
164 $this->extractConfig2( $info );
165 } else {
166 // $version === 1
167 $this->extractConfig1( $info );
168 }
169 $this->extractHooks( $info );
170 $dir = dirname( $path );
171 $this->extractExtensionMessagesFiles( $dir, $info );
172 $this->extractMessagesDirs( $dir, $info );
173 $this->extractNamespaces( $info );
174 $this->extractResourceLoaderModules( $dir, $info );
175 $this->extractParserTestFiles( $dir, $info );
176 if ( isset( $info['callback'] ) ) {
177 $this->callbacks[] = $info['callback'];
178 }
179
180 $this->extractCredits( $path, $info );
181 foreach ( $info as $key => $val ) {
182 if ( in_array( $key, self::$globalSettings ) ) {
183 $this->storeToArray( $path, "wg$key", $val, $this->globals );
184 // Ignore anything that starts with a @
185 } elseif ( $key[0] !== '@' && !in_array( $key, self::$notAttributes )
186 && !in_array( $key, self::$creditsAttributes )
187 ) {
188 $this->storeToArray( $path, $key, $val, $this->attributes );
189 }
190 }
191 }
192
193 public function getExtractedInfo() {
194 // Make sure the merge strategies are set
195 foreach ( $this->globals as $key => $val ) {
196 if ( isset( self::$mergeStrategies[$key] ) ) {
197 $this->globals[$key][ExtensionRegistry::MERGE_STRATEGY] = self::$mergeStrategies[$key];
198 }
199 }
200
201 return [
202 'globals' => $this->globals,
203 'defines' => $this->defines,
204 'callbacks' => $this->callbacks,
205 'credits' => $this->credits,
206 'attributes' => $this->attributes,
207 ];
208 }
209
210 public function getRequirements( array $info ) {
211 $requirements = [];
212 $key = ExtensionRegistry::MEDIAWIKI_CORE;
213 if ( isset( $info['requires'][$key] ) ) {
214 $requirements[$key] = $info['requires'][$key];
215 }
216
217 return $requirements;
218 }
219
220 protected function extractHooks( array $info ) {
221 if ( isset( $info['Hooks'] ) ) {
222 foreach ( $info['Hooks'] as $name => $value ) {
223 if ( is_array( $value ) ) {
224 foreach ( $value as $callback ) {
225 $this->globals['wgHooks'][$name][] = $callback;
226 }
227 } else {
228 $this->globals['wgHooks'][$name][] = $value;
229 }
230 }
231 }
232 }
233
234 /**
235 * Register namespaces with the appropriate global settings
236 *
237 * @param array $info
238 */
239 protected function extractNamespaces( array $info ) {
240 if ( isset( $info['namespaces'] ) ) {
241 foreach ( $info['namespaces'] as $ns ) {
242 $id = $ns['id'];
243 $this->defines[$ns['constant']] = $id;
244 $this->attributes['ExtensionNamespaces'][$id] = $ns['name'];
245 if ( isset( $ns['gender'] ) ) {
246 $this->globals['wgExtraGenderNamespaces'][$id] = $ns['gender'];
247 }
248 if ( isset( $ns['subpages'] ) && $ns['subpages'] ) {
249 $this->globals['wgNamespacesWithSubpages'][$id] = true;
250 }
251 if ( isset( $ns['content'] ) && $ns['content'] ) {
252 $this->globals['wgContentNamespaces'][] = $id;
253 }
254 if ( isset( $ns['defaultcontentmodel'] ) ) {
255 $this->globals['wgNamespaceContentModels'][$id] = $ns['defaultcontentmodel'];
256 }
257 if ( isset( $ns['protection'] ) ) {
258 $this->globals['wgNamespaceProtection'][$id] = $ns['protection'];
259 }
260 if ( isset( $ns['capitallinkoverride'] ) ) {
261 $this->globals['wgCapitalLinkOverrides'][$id] = $ns['capitallinkoverride'];
262 }
263 }
264 }
265 }
266
267 protected function extractResourceLoaderModules( $dir, array $info ) {
268 $defaultPaths = isset( $info['ResourceFileModulePaths'] )
269 ? $info['ResourceFileModulePaths']
270 : false;
271 if ( isset( $defaultPaths['localBasePath'] ) ) {
272 if ( $defaultPaths['localBasePath'] === '' ) {
273 // Avoid double slashes (e.g. /extensions/Example//path)
274 $defaultPaths['localBasePath'] = $dir;
275 } else {
276 $defaultPaths['localBasePath'] = "$dir/{$defaultPaths['localBasePath']}";
277 }
278 }
279
280 foreach ( [ 'ResourceModules', 'ResourceModuleSkinStyles' ] as $setting ) {
281 if ( isset( $info[$setting] ) ) {
282 foreach ( $info[$setting] as $name => $data ) {
283 if ( isset( $data['localBasePath'] ) ) {
284 if ( $data['localBasePath'] === '' ) {
285 // Avoid double slashes (e.g. /extensions/Example//path)
286 $data['localBasePath'] = $dir;
287 } else {
288 $data['localBasePath'] = "$dir/{$data['localBasePath']}";
289 }
290 }
291 if ( $defaultPaths ) {
292 $data += $defaultPaths;
293 }
294 $this->globals["wg$setting"][$name] = $data;
295 }
296 }
297 }
298 }
299
300 protected function extractExtensionMessagesFiles( $dir, array $info ) {
301 if ( isset( $info['ExtensionMessagesFiles'] ) ) {
302 $this->globals["wgExtensionMessagesFiles"] += array_map( function( $file ) use ( $dir ) {
303 return "$dir/$file";
304 }, $info['ExtensionMessagesFiles'] );
305 }
306 }
307
308 /**
309 * Set message-related settings, which need to be expanded to use
310 * absolute paths
311 *
312 * @param string $dir
313 * @param array $info
314 */
315 protected function extractMessagesDirs( $dir, array $info ) {
316 if ( isset( $info['MessagesDirs'] ) ) {
317 foreach ( $info['MessagesDirs'] as $name => $files ) {
318 foreach ( (array)$files as $file ) {
319 $this->globals["wgMessagesDirs"][$name][] = "$dir/$file";
320 }
321 }
322 }
323 }
324
325 /**
326 * @param string $path
327 * @param array $info
328 * @throws Exception
329 */
330 protected function extractCredits( $path, array $info ) {
331 $credits = [
332 'path' => $path,
333 'type' => isset( $info['type'] ) ? $info['type'] : 'other',
334 ];
335 foreach ( self::$creditsAttributes as $attr ) {
336 if ( isset( $info[$attr] ) ) {
337 $credits[$attr] = $info[$attr];
338 }
339 }
340
341 $name = $credits['name'];
342
343 // If someone is loading the same thing twice, throw
344 // a nice error (T121493)
345 if ( isset( $this->credits[$name] ) ) {
346 $firstPath = $this->credits[$name]['path'];
347 $secondPath = $credits['path'];
348 throw new Exception( "It was attempted to load $name twice, from $firstPath and $secondPath." );
349 }
350
351 $this->credits[$name] = $credits;
352 $this->globals['wgExtensionCredits'][$credits['type']][] = $credits;
353 }
354
355 /**
356 * Set configuration settings for manifest_version == 1
357 * @todo In the future, this should be done via Config interfaces
358 *
359 * @param array $info
360 */
361 protected function extractConfig1( array $info ) {
362 if ( isset( $info['config'] ) ) {
363 if ( isset( $info['config']['_prefix'] ) ) {
364 $prefix = $info['config']['_prefix'];
365 unset( $info['config']['_prefix'] );
366 } else {
367 $prefix = 'wg';
368 }
369 foreach ( $info['config'] as $key => $val ) {
370 if ( $key[0] !== '@' ) {
371 $this->globals["$prefix$key"] = $val;
372 }
373 }
374 }
375 }
376
377 /**
378 * Set configuration settings for manifest_version == 2
379 * @todo In the future, this should be done via Config interfaces
380 *
381 * @param array $info
382 */
383 protected function extractConfig2( array $info ) {
384 if ( isset( $info['config_prefix'] ) ) {
385 $prefix = $info['config_prefix'];
386 } else {
387 $prefix = 'wg';
388 }
389 if ( isset( $info['config'] ) ) {
390 foreach ( $info['config'] as $key => $data ) {
391 $value = $data['value'];
392 if ( isset( $value['merge_strategy'] ) ) {
393 $value[ExtensionRegistry::MERGE_STRATEGY] = $value['merge_strategy'];
394 }
395 $this->globals["$prefix$key"] = $value;
396 }
397 }
398 }
399
400 protected function extractParserTestFiles( $dir, array $info ) {
401 if ( isset( $info['ParserTestFiles'] ) ) {
402 foreach ( $info['ParserTestFiles'] as $path ) {
403 $this->globals['wgParserTestFiles'][] = "$dir/$path";
404 }
405 }
406 }
407
408 /**
409 * @param string $path
410 * @param string $name
411 * @param array $value
412 * @param array &$array
413 * @throws InvalidArgumentException
414 */
415 protected function storeToArray( $path, $name, $value, &$array ) {
416 if ( !is_array( $value ) ) {
417 throw new InvalidArgumentException( "The value for '$name' should be an array (from $path)" );
418 }
419 if ( isset( $array[$name] ) ) {
420 $array[$name] = array_merge_recursive( $array[$name], $value );
421 } else {
422 $array[$name] = $value;
423 }
424 }
425
426 public function getExtraAutoloaderPaths( $dir, array $info ) {
427 $paths = [];
428 if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
429 $path = "$dir/vendor/autoload.php";
430 if ( file_exists( $path ) ) {
431 $paths[] = $path;
432 }
433 }
434 return $paths;
435 }
436 }